/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.upn.sigemfi.vnf.bc;

import edu.upn.sigemfi.seg.bc.AgenciaFacadeLocal;
import edu.upn.sigemfi.seg.bc.UsuarioFacadeLocal;
import edu.upn.sigemfi.seg.be.Agencia;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Administrador
 */
public class AgenciaDelegate {

    private AgenciaFacadeLocal local;

    public AgenciaDelegate(){
        local = lookupAgenciaFacade();
    }

    public List<Agencia> listar() throws Exception{
        try {
            return local.findAll();
        } catch (Exception e) {
            local = lookupAgenciaFacade();
            throw new Exception("Listar Agencias." + e.getMessage(), e);
        }
    }

    private AgenciaFacadeLocal lookupAgenciaFacade() {
        try {
            Context c = new InitialContext();
            return (AgenciaFacadeLocal) c.lookup("java:comp/env/AgenciaFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    public Agencia buscarPorId(Long id) throws Exception {
        Agencia moneda = null;
        try {
            moneda = local.find(id);
        } catch (Exception ex) {
            local = lookupAgenciaFacade();
            throw new Exception("Buscar Agencia... " + ex.getMessage());
        }
        return moneda;
    }

    private UsuarioFacadeLocal lookupUsuarioFacade() {
        try {
            Context c = new InitialContext();
            return (UsuarioFacadeLocal) c.lookup("java:comp/env/UsuarioFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

}
